<?php

namespace RTSCraft\MainBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

use RTSCraft\MainBundle\Entity\Element;
use RTSCraft\MainBundle\Entity\Prices;
use RTSCraft\MainBundle\Entity\Sheets;

class EntityDisplayController extends Controller
{
  /**
   * @Route("/entity_display", name="operation")
   * @Template()
   */
  public function simpleGetAction()
  {
    $req = $this->get('request');
    $em = $this->getDoctrine()->getEntityManager();

    if ($req->get('entity') != null)
      $e = $this->getElementByName($req->get('entity'));
    else
      $e = null;

    return $this->render('RTSCraftMainBundle:Admin:entity_display_base.html.twig', array ("entity" => $e));
  }

  /**
   * @Route("/entity_edit", name="operation")
   * @Template()
   */
  public function editGetAction()
  {
    $req = $this->get('request');
    $em = $this->getDoctrine()->getEntityManager();

    if ($req->get('entity') != null)
      $e = $this->getElementByName($req->get('entity'));
    else
      $e = null;

    return $this->render('RTSCraftMainBundle:Admin:entity_display_editor.html.twig', array ("entity" => $e,
               "resources" => $this->getResources(),
	       "factions" => $this->getFactions(),
	       "statistics" => $this->getStatistics()));
  }

  /**
   * @Route("/entity_data_add", name="operation")
   * @Template()
   */
  public function dataAddAction()
  {
    $entity = null;
    if ($this->get('request')->getMethod() == "POST")
      {
	$req = $this->get('request')->request;
	$entity_name = $req->get("action_entity");
	$entity = $this->getElementByName($entity_name);
	$em = $this->getDoctrine()->getEntityManager();

	switch ($req->get("action_type"))
	  {
	  case "image":
	    $entity->setImgUrl($req->get('field'));
	    $em->persist($entity);
	    $em->flush();
	    break;
	  case "resource":
	    $c = $entity->getCosts();
	    $c = $c[0];
	    $p = new Prices();
	    $p->setValue($req->get('field'));
	    $p->setRessource($this->getResourceByName($req->get('action_select')));
	    $em->persist($p);
	    $c->addPrices($p);
	    $em->persist($c);
	    $em->persist($entity);
	    $em->flush();
	    break;
	  case "statistic":
	    $n = new Sheets();
	    $n->setValue($req->get('field'));
	    $n->addStatistics($this->getStatisticByName($req->get('action_select')));
	    $em->persist($n);
	    $entity->addSheets($n);
	    $em->persist($entity);
	    $em->flush();
	    break;
	  case "faction":
	    $entity->addFactions($this->getFactionByName($req->get('action_select')));
	    $em->persist($entity);
	    $em->flush();
	    break;
	  }
      }

    return $this->render('RTSCraftMainBundle:Admin:entity_display_editor.html.twig', array ("entity" => $entity,
               "resources" => $this->getResources(),
	       "factions" => $this->getFactions(),
	       "statistics" => $this->getStatistics()));
  }

  /**
   * @Route("/entity_data_del", name="operation")
   * @Template()
   */
  public function dataDelAction()
  {
    if ($this->get('request')->getMethod() == "POST")
      {
	$req = $this->get('request')->request;
	$entity_name = $req->get("action_entity");
	$entity = $this->getElementByName($entity_name);
	$value = $req->get("action_value");
	$em = $this->getDoctrine()->getEntityManager();
	switch ($req->get("action_type"))
	  {
	  case "faction":
	    $f = $this->getFactionByName($value);
	    $co = $em->getConnection();
	    $co->exec("DELETE FROM element_factions WHERE " .
			 " factions_id = " . $f->getId() . " AND " .
			 " element_id = " . $entity->getId());
	    break;
	  case "statistic":
	    $s = $this->getSheetByStatistic($value);
	    $co = $em->getConnection();
	    $co->exec("DELETE FROM element_sheets WHERE " .
			 " sheets_id = " . $s->getId() . " AND " .
			 " element_id = " . $entity->getId());
	    $co->exec("DELETE FROM Sheets WHERE " .
	    	      " id = " . $s->getId());

	    break;
	  case "resource":
	    $p = $this->getPriceByResource($value);
	    $c = $entity->getCosts();
	    $c = $c[0];
	    $co = $em->getConnection();
	    $co->exec("DELETE FROM costs_prices WHERE " .
			 " costs_id = " . $c->getId() . " AND " .
			 " prices_id = " . $p->getId());
	    /* $co->exec("DELETE FROM Costs WHERE " . */
	    /* 	      " id = " . $c->getId()); */
	    break;

	  }
      }

    return $this->render('RTSCraftMainBundle:Admin:entity_display_editor.html.twig', array ("entity" => $entity,
               "resources" => $this->getResources(),
	       "factions" => $this->getFactions(),
	       "statistics" => $this->getStatistics()));

  }

  public function getElementByName($n)
  {
    $repo = $this->getDoctrine()->getRepository('RTSCraftMainBundle:Element');
    
    $results = $repo->findByName($n);
    return $results[0];
  }

  public function getSheetByStatistic($n)
  {
    $repo = $this->getDoctrine()->getRepository('RTSCraftMainBundle:Element');
    
    $results = $repo->findSheetByStatistic($n);
    return $results;
  }

  public function getPriceByResource($n)
  {
    $repo = $this->getDoctrine()->getRepository('RTSCraftMainBundle:Element');
    
    $results = $repo->findPriceByResource($n);
    return $results;
  }

  public function getResources()
  {
    $repo = $this->getDoctrine()->getRepository('RTSCraftMainBundle:Ressources');
    
    $results = $repo->findBy(array("deleted" => false));
    
    return $results;
  }

  public function getFactions()
  {
    $repo = $this->getDoctrine()->getRepository('RTSCraftMainBundle:Factions');
    
    $results = $repo->findBy(array("deleted" => false));
      
    return $results;
  }

  public function getStatistics()
  {
    $repo = $this->getDoctrine()->getRepository('RTSCraftMainBundle:Statistics');
    
    $results = $repo->findBy(array("deleted" => false));
      
    return $results;
  }

  public function getResourceByName($n)
  {
    $repo = $this->getDoctrine()->getRepository('RTSCraftMainBundle:Ressources');
    
    $results = $repo->findByName($n);
    return $results[0];
  }

  public function getFactionByName($n)
  {
    $repo = $this->getDoctrine()->getRepository('RTSCraftMainBundle:Factions');
    
    $results = $repo->findByName($n);
    return $results[0];
  }

  public function getStatisticByName($n)
  {
    $repo = $this->getDoctrine()->getRepository('RTSCraftMainBundle:Statistics');
    
    $results = $repo->findByName($n);
    return $results[0];
  }

}
